from flask import Flask
from flask_sqlalchemy import  SQLAlchemy
from flask_script import  Manager
from flask_wtf import CSRFProtect
from flask_session import Session
from flask_migrate import Migrate,MigrateCommand
import redis


class Config(object):

    DEBUG = True  # 开启调试模式

    SQLALCHEMY_DATABASE_URI = "mysql://root:mysql@127.0.0.1:3306/python20"  # 配置数据库
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    REDIS_HOST = '127.0.0.1'  # 配置ｒｅｄｉｓ主机和端口号
    REDIS_PORT = 6379



    SECRTE_KEY = 'asdfjgkhhjjhg'  # 设置session秘钥
    SESSION_TYPE = 'redis'  # 给session设置redis存储

    SESSION_USE_SIGNER = True  # 使用session签名
    SESSION_REDIS = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT)
    SESSION_PERMANENT = 86400 * 3  # 设置session有效期，单位是秒，86400s是一天

app = Flask(__name__)

app.config.from_object(Config)  # 给app对象添加配置

db = SQLAlchemy(app)  # 给app设置数据库

CSRFProtect(app)  # 开启CSRF保护

Session(app)  # 给app设置session, 目的是持久化操作，不需次次登录， 把session存到redis里

redis_store = redis.StrictRedis(host=Config.REDIS_HOST,port=Config.REDIS_PORT, decode_responses=True)  # 创建redis（存储验证信息）

manager = Manager(app)  # 用manager管理app
Migrate(app, db)  # 创建数据库的迁移框架
manager.add_command('mysql', MigrateCommand)  # 添加迁移数据库框架的脚本


@app.route('/')
def index():

    return 'index 111'


if __name__ == '__main__':

    manager.run()